<template>
	<view class="goods_list">
		<good-list  @goodsItemClick="goGoodsDetail" :goods="goods"></good-list>
	
		<view v-if="flag" class="isOver">----我是有底线的----</view>
	</view>
</template>

<script>
	import goodsList from '../../components/good-list/good-list.vue'
	export default {
		data() {
			return {
				pageindex:1,
				goods:[],
				flag:false
			}
		},
		// 注册组件
		components:{
			"goods-list":goodsList
		},
		onLoad() {
			this.getGoodsList()
		},
		methods: {
			// 获取热门商品列表数据
			async getGoodsList(callBack){
				const res = await this.$myRequest({
					url:'/api/getgoods?pageindex='+this.pageindex
				})		
				// ES6写法，两个数组合并
				this.goods = [...this.goods,...res.data.message]
				callBack && callBack()
			},
			// 商品详情
			goGoodsDetail(id){
				uni.navigateTo({
					url:'../goodsDetail/goodsDetail?id='+id
				})
			},
		},
		// 触底触发事件
		onReachBottom() {
			if(this.goods.length<this.pageindex*10) return this.flag = true
			this.pageindex++
			this.getGoodsList()
			
		},
		// 下拉刷新
		onPullDownRefresh() {
			console.log('下来刷新了')
			this.pageindex=1,
			this.goods=[],
			this.flag=false
			setTimeout(()=>{
				this.getGoodsList(()=>{
					uni.stopPullDownRefresh()
				})
			},800)
		}
	}
</script>

<style lang="scss">
	.goods_list{
		 background: #eee;
		 .isOver{
			 color:#00aa00;
			 text-align: center;
			 font-size: 25rpx;
			 height: 50px;
			 line-height: 50px;
			 width: 100%;
		 }
			
	}
</style>
